/*package cn.wx.scholar.es.service;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.FuzzyQueryBuilder;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.util.Assert;

import cn.wx.common.utils.JSONUtils;

*//**
 * es 的服务类基类
 * @author Administrator
 *//*
public abstract class EsBaseService {
	
	Logger logger = LoggerFactory.getLogger(EsBaseService.class);
	
	protected final static float tieBreaker = 0.3f;
	protected final static String type = "best_fields";
	protected final static String minimumShouldMatch = "30%";
	protected final static String analyzer = "ik_max_word";
	protected final static String ESINDEX = "esIndex";
	@Autowired
	private ElasticsearchTemplate elasticsearchTemplate;
	
	*//**
	 * 查询封装
	 *//*
	protected <T> Page<T> queryPage(SearchQuery searchQuery, Class<T> clazz) {
		return elasticsearchTemplate.query(searchQuery, response -> {
			return mapResults(response, clazz, searchQuery.getPageable());
        });
	}
	
	private <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
		
		long totalHits = response.getHits().getTotalHits();
		float maxScore = response.getHits().getMaxScore();

		List<T> results = new ArrayList<>();
		
		for (SearchHit hit : response.getHits()) {
			try{
				if (hit != null) {
					String esIndex = hit.getIndex();
					T result = JSONUtils.p(hit.getSourceAsString(), clazz);
					Field f = result.getClass().getSuperclass().getDeclaredField(ESINDEX);
			        f.setAccessible(true);
			        f.set(result, esIndex);
					results.add(result);
				}
			}catch (Exception e) {
				results.clear();
				logger.info("数据转化异常 :" + hit.getIndex() + ",source:" +hit.getSourceAsString());
			}

		}
		
		return new AggregatedPageImpl<T>(results, pageable, totalHits, response.getAggregations(), response.getScrollId(),
				maxScore);
	}
	
	
	protected SearchQuery getSearchQueryWithField(String value, String search_field, Pageable pageable, String... indices) {
		
		Assert.notNull(indices, "No index defined for Query");
		Assert.notNull(value, "No value defined for Query");
		Assert.notNull(search_field, "No search_field defined for Query");
		
		NativeSearchQueryBuilder searchQueryBuildera = new NativeSearchQueryBuilder();
		final String kw = value.trim();
		
		FuzzyQueryBuilder builder = QueryBuilders.fuzzyQuery(search_field, kw);
		searchQueryBuildera.withQuery(builder);
		
		if(pageable == null) {
			pageable = PageRequest.of(0, 15);
		}
		searchQueryBuildera.withPageable(pageable);
		searchQueryBuildera.withSearchType(SearchType.QUERY_THEN_FETCH);
		searchQueryBuildera.withIndices(indices);
		SearchQuery searchQuery = searchQueryBuildera.build();
		
		return searchQuery;
		
	}
	
	*//**
	 * 获取查询对象
	 * @return
	 *//*
	protected SearchQuery getSearchQueryWithFields(String value, String[] search_field, Pageable pageable, String... indices) {
		
		Assert.notNull(indices, "No index defined for Query");
		Assert.notNull(value, "No value defined for Query");
		Assert.notNull(search_field, "No search_field defined for Query");
		
		NativeSearchQueryBuilder searchQueryBuildera = new NativeSearchQueryBuilder();
		final String kw = value.trim();
		

		BoolQueryBuilder builder = QueryBuilders.boolQuery();
		MultiMatchQueryBuilder mb = QueryBuilders.multiMatchQuery(kw, search_field);
		mb.minimumShouldMatch(minimumShouldMatch)
		.type(type)
		.tieBreaker(tieBreaker)
		.analyzer(analyzer)
		.fuzziness(Fuzziness.AUTO)
		.prefixLength(kw.length()-1);
		builder.must(mb);
		searchQueryBuildera.withQuery(builder);
		

		*//**
		 * 当分页参数为空时，默认查15个
		 *//*
		if(pageable == null) {
			pageable = PageRequest.of(0, 15);
		}
		searchQueryBuildera.withPageable(pageable);
		searchQueryBuildera.withSearchType(SearchType.QUERY_THEN_FETCH);
		searchQueryBuildera.withIndices(indices);
		SearchQuery searchQuery = searchQueryBuildera.build();
		
		return searchQuery;
	}
	
	
	
	
}
*/